package com.hk.yingmi;

import com.hk.commons.RedisCilent;
import com.hk.commons.entity.yingmi.YingmiBrokerProduct;
import com.hk.service.yingmi.YingmiProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Antinomy on 17/9/27.
 */
@Service
public class YingmiProductCache {

    public static final String YINGMI_BROKER_PRODUCT_LIST = "YINGMI_BROKER_PRODUCT_LIST";

    @Autowired
    private YingmiProductService yingmiProductService;

    public Boolean isBrokerProduct(String fundCode) {

        if (RedisCilent.existsKey(YINGMI_BROKER_PRODUCT_LIST)) {
            return RedisCilent.hasValue(YINGMI_BROKER_PRODUCT_LIST, fundCode);
        }

        List<YingmiBrokerProduct> products = yingmiProductService.listAllBrokerProduct();

        products.parallelStream()
                .forEach(product -> {

                    if (!RedisCilent.hasValue(YINGMI_BROKER_PRODUCT_LIST, product.getFundCode())) {
                        RedisCilent.sadd(YINGMI_BROKER_PRODUCT_LIST, product.getFundCode());
                    }

                });


        return RedisCilent.hasValue(YINGMI_BROKER_PRODUCT_LIST, fundCode);
    }
}
